package cc.telecomdigital.tdstock.Framework;


import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityController;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;

public abstract class ActivityGroup_Framework extends
		Abstract_Activity_Framework<ActivityGroupController>
{
	/** The Class use control list and should have a host list to implement.
	 *  Implement your where is your static host list
	 * @return
	 */
	protected abstract ActivityGroupController GetActivityGroupController();	
	
	@Override
	protected ActivityGroupController GetActivityController()
	{
		return GetActivityGroupController();
	}

	/** The Application Main Controller List, it could be overridden
	 * @return
	 */
	protected ActivityController GetAppActivityController()
	{
		return appHost.actListController;
	}

	/** Switch to the Top of this Group Host Top, remain with the same Activity, but on primary Activity
	    with intent parameter bring in **/
	protected void SwitchToTopGroupActivity(Intent iIntentObj)
	{
		GetActivityGroupController().ActivityBackwardSwitchToTop(this, GetActivityGroupController().GetActivityClassOf(0), 
													  iIntentObj);
	}

	/** Switch to the Top of this Group Host Top, remain with the same Activity, but on primary Activity **/	
	protected void SwitchToTopGroupActivity()
	{
		SwitchToTopGroupActivity(new Intent());
	}	
		
	/** Switch away from this Group and move to the previous non-group activity display, 
	 *  work similar to SwitchActivityFromHost, but always comes with backward animation
	 *  with intent parameter brings in.
	 *  Direction pair with SwitchActivityFromHost **/
	protected void SwitchBackToTheHost(Intent iIntentObj)
	{
		GetAppActivityController().ActivityBackwardSwitch(this, iIntentObj);
	}

	/** Switch away from this Group and move to the previous application display, 
	 *  work similar to SwitchActivityFromHost, but always comes with backward animation 
	 *  Direction pair as SwitchActivityFromHost **/	
	protected void SwitchBackToTheHost()
	{
		SwitchBackToTheHost(new Intent());
	}
	
	/** Switch away from this host to a new interface, with intent parameter
	 * Direction Pair as SwitchBackToTheHost
	 * @param iActivityClass - to the Main List Activity that you desire
	 * @param iIntentObj
	 */
	protected void SwitchActivityFromHost(Class<? extends Activity> iActivityClass, Intent iIntentObj)
	{
			
		if (iActivityClass.equals(getClass()))
			return;

		/** Do not play with self **/
		Activity                   parentActivity;		

		parentActivity  = getParent();
		if (parentActivity != null)
		{
			if (parentActivity.getParent() instanceof TabActivity)
				parentActivity = parentActivity.getParent();
		}
			
		GetAppActivityController().ActivityFreeSwitch(parentActivity, iActivityClass, iIntentObj);
	}
	
	/** Switch away from this host to a new interface 
	 *  Such that you do not want any parent switch, instead
	 *  you are switching our from your Group
	 * Direction Pair as SwitchBackToTheHost  **/
	protected void SwitchActivityFromHost(Class<? extends Activity> iActivityClass)
	{
		SwitchActivityFromHost(iActivityClass, new Intent());
	}	
		
	/** Specially design to work under TabActivity, it will set the tab to the assined one (ActivityGroupHost_Framework)
	 * then call again to change the UI under that group
	 * @param iTabIndex - The index for the Host Activity, on the Tab assignment
	 * @param iNewController - Controller for the Host activity
	 * @param iSubActivityClass - ActivityGroup_Framework window that you are looking for
	 * @param iIntentObj - You intent parameter
	 */
	protected boolean SwitchTabHostActivity( int iTabIndex, ActivityGroupController iNewController,
								   Class<? extends ActivityGroup_Framework> iSubActivityClass, 
								   Intent iIntentObj)
	{
		try
		{
			if (iNewController == null)
				return false;
			
			boolean isTransition;
			TabActivity parentAct;
			
			isTransition = iNewController.IsTransitWithAnimation();
			iNewController.TransitWithAnimation(false);
			iNewController.ActivityFreeSwitch(iNewController.GetHostActivity(), iSubActivityClass, iIntentObj);	
			iNewController.TransitWithAnimation(isTransition);
			
			parentAct = (TabActivity)getParent().getParent();				
			parentAct.getTabHost().setCurrentTab(iTabIndex);			
		}
		catch(Exception e)
		{
			TDStockLog.e(GetIdentity(), "SwitchTabHostActivity: " + e.toString());
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	/** Specially design to work under TabActivity, it will set the tab to the assined one (ActivityGroupHost_Framework)
	 * then call again to change the UI under that group
	 * @param iTabIndex - The index for the Host Activity, on the Tab assignment
	 * @param iNewController - Controller for the Host activity
	 * @param iSubActivityClass - ActivityGroup_Framework window that you are looking for
	 */	
	protected boolean SwitchTabHostActivity( int iTabIndex, ActivityGroupController iNewController,
			   Class<? extends ActivityGroup_Framework> iSubActivityClass )
	{
		return SwitchTabHostActivity(iTabIndex, iNewController, iSubActivityClass, new Intent());
	}
	
	/** Intentionally to leave empty
	 *  This is not allow the use to use the super.onCreateOptions,
	 *  Since the group host will ask the extends type of this for menu
	 *  when creating them
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		//add menu here
		//do not call super.onCreateOptionsMenu(menu) here
	    return true;
	}

	/** Intentionally to leave empty
	 *  This is not allow the use to use the super.onCreateOptions,
	 *  Since the group host will ask the extends type of this for menu
	 *  when creating them
	 */	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		//handle on menu item selected here
		//do not call onOptionsItemSelected(item) here
	    return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		//handle on menu item selected here
		//do not call super.onMenuItemSelected(featureId, item) here
	    return true;
	}	
}
